package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "购物车相关接口")
public class ShoppingCarController {
    @Autowired
    ShoppingCarService shoppingCarService;


    @PostMapping("/add")
    @ApiOperation("购物车添加接口")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车，商品信息为："+shoppingCartDTO);
        shoppingCarService.add(shoppingCartDTO);
        return Result.success();
    }
    @PostMapping("/sub")
    @ApiOperation("购物车删除接口")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车，商品信息为："+shoppingCartDTO);
        shoppingCarService.sub(shoppingCartDTO);
        return Result.success();
    }
    ///user/shoppingCart/list
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List <ShoppingCart>> get(){
        log.info("查看用户的购物车，用户的id:"+ BaseContext.getCurrentId());
        return Result.success(shoppingCarService.get());
    }
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result  clean(){
        log.info("删除用户的购物车，用户的id:"+ BaseContext.getCurrentId());
        shoppingCarService.clean();
        return Result.success();
    }

}
